package it.polimi.swKnights.SWIMv2.utility;

import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class MailSender
{

	public void sendMail(String to)
	{
		final String username = "swimv2.swknights@gmail.com";
		final String password = "sangiorgio";

		Properties props = new Properties();
		props.put("mail.smtp.auth", "true");
		props.put("mail.smtp.starttls.enable", "true");
		props.put("mail.smtp.host", "smtp.gmail.com");
		props.put("mail.smtp.port", "587");

		Session session = Session.getInstance(props,
				new javax.mail.Authenticator()
				{
					protected PasswordAuthentication getPasswordAuthentication()
					{
						return new PasswordAuthentication(username, password);
					}
				});

		try
		{

			Message message = new MimeMessage(session);
			message.setFrom(new InternetAddress("SWIMv2-Registration-Manager"));
			message.setRecipients(Message.RecipientType.TO,
					InternetAddress.parse(to));

			message.setSubject("Swimv2 - Confirm Registration");

			if (this.message == null || this.message.length() < 2)
				this.setDebugMessage();
			message.setContent(this.message, "text/html");

			Transport.send(message);

		}
		catch (MessagingException e)
		{
			throw new RuntimeException(e);
		}
	}

	public String getMessage()
	{
		return message;
	}

	public void setMessage(String message)
	{
		this.message = message;
	}

	private void setDebugMessage()
	{
		this.message = "<h1>TETTE GIGANTI A TUTTI!!</h1> <br /> <br /> "
				+ "E non dimenticare di apripre britney_nuda.exe in allegato!! ;) ";
		throw new IllegalArgumentException();
	}

	private String message;
}
